﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Kooboo.Commerce.AddIn;
using Kooboo.IoC;

namespace Kooboo.Commerce.Repositories
{
    partial class ProductAttribute:Entity, IProductAttribute
    {

        #region IProductAttribute Members

        IProductType _ProductType_For_Entity;
        IProductType IProductAttribute.ProductType
        {
            get
            {
                if (this._ProductType_For_Entity == null)
                {
                    this._ProductType_For_Entity = this.DataContext.ProductTypes
                        .Where(i => i.Id == this.ProductTypeId)
                        .FirstOrDefault();
                }

                return this._ProductType_For_Entity;
            }
            set
            {
                this._ProductType_For_Entity = value;
                this.ProductTypeId = value.Id;
            }
        }

        DataFormat? _DataFormat_For_Entity;
        DataFormat IProductAttribute.DataFormat
        {
            get
            {
                if (this._DataFormat_For_Entity == null)
                {
                    this._DataFormat_For_Entity = (DataFormat)this.DataFormat;
                }

                return this._DataFormat_For_Entity.Value;
            }
            set
            {
                this._DataFormat_For_Entity = value;
                this.DataFormat = (int)value;
            }
        }
        IWebControl _WebControl;
        public IWebControl WebControl
        {
            get
            {
                if (this._WebControl == null)
                {
                    this._WebControl = ObjectContainer.CreateInstances<IWebControl>()
                        .Where(i => i.Name == this.WebControlName)
                        .FirstOrDefault();
                }
                return this._WebControl;
            }
            set
            {
                this._WebControl = value;
                this.WebControlName = value.Name;
            }
        }

        List<IValidationRule> _ValidationRules_For_Entity;
        List<IValidationRule> IProductAttribute.ValidationRules
        {
            get
            {
                if (this._ValidationRules_For_Entity == null)
                {
                    this._ValidationRules_For_Entity = this.DataContext.ValidationRules
                        .Where(i => i.ProductAttributeId == this.Id)
                        .OfType<IValidationRule>()
                        .ToList();
                }

                return this._ValidationRules_For_Entity;
            }
            set
            {
                this._ValidationRules_For_Entity = value;
            }
        }

        #endregion
    }
}
